package com.ujs.edu.medicine.dao;

import com.ujs.edu.medicine.dataobject.YunjiayiPrescription;

import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface YunjiayiPrescriptionMapper {
    @Delete({
            "delete from yunjiayi_prescription",
            "where prescriptionID = #{prescriptionid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer prescriptionid);

    /*通过UID删除*/
    @Delete({
            "delete from yunjiayi_like",
            "where userId = #{userId,jdbcType=INTEGER}"
    })
    int deleteByUID(Integer patientID);

    @Insert({
            "insert into yunjiayi_prescription (prescriptionID, patientID, ",
            "doctorID, creatime, ",
            "medicine, decription, ",
            "prescriptionstatus)",
            "values (#{prescriptionid,jdbcType=INTEGER}, #{patientid,jdbcType=INTEGER}, ",
            "#{doctorid,jdbcType=INTEGER}, now(), ",
            "#{medicine,jdbcType=VARCHAR}, #{decription,jdbcType=VARCHAR}, ",
            "#{prescriptionstatus,jdbcType=VARCHAR})"
    })
    @Options(useGeneratedKeys = true,keyProperty = "prescriptionid",keyColumn = "prescriptionID")
    int insert(YunjiayiPrescription record);

    @Select({
            "select",
            "prescriptionID, patientID, doctorID, creatime, medicine, decription, prescriptionstatus",
            "from yunjiayi_prescription",
            "where prescriptionID = #{prescriptionid,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "prescriptionID", property = "prescriptionid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "patientID", property = "patientid", jdbcType = JdbcType.INTEGER),
            @Result(column = "doctorID", property = "doctorid", jdbcType = JdbcType.INTEGER),
            @Result(column = "creatime", property = "creatime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "medicine", property = "medicine", jdbcType = JdbcType.VARCHAR),
            @Result(column = "decription", property = "decription", jdbcType = JdbcType.VARCHAR),
            @Result(column = "prescriptionstatus", property = "prescriptionstatus", jdbcType = JdbcType.VARCHAR)
    })
    YunjiayiPrescription selectByPrimaryKey(Integer prescriptionid);

    @Select({
            "select",
            "prescriptionID, patientID, doctorID, creatime, medicine, decription, prescriptionstatus",
            "from yunjiayi_prescription"
    })
    @Results(id = "prescriptionMap", value = {
            @Result(column = "prescriptionID", property = "prescriptionid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "patientID", property = "patientid", jdbcType = JdbcType.INTEGER),
            @Result(column = "doctorID", property = "doctorid", jdbcType = JdbcType.INTEGER),
            @Result(column = "creatime", property = "creatime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "medicine", property = "medicine", jdbcType = JdbcType.VARCHAR),
            @Result(column = "decription", property = "decription", jdbcType = JdbcType.VARCHAR),
            @Result(column = "prescriptionstatus", property = "prescriptionstatus", jdbcType = JdbcType.VARCHAR)
    })
    List<YunjiayiPrescription> selectAll();

    /*模糊查询*/
    @Select("select * from yunjiayi_prescription where decription like '%${decription}%'")
    List<YunjiayiPrescription> searchLikePres(@Param("decription")String decription);

    @Select("select prescriptionID, patientID, doctorID, creatime, medicine, decription from yunjiayi_prescription where doctorID=#{doctorID,jdbcType=INTEGER}")
    @ResultMap("prescriptionMap")
    List<YunjiayiPrescription> getPrescriptionById(Integer doctorID);

    /*分页查询*/
    @Select("select prescriptionID, patientID, doctorID, creatime, medicine, decription from yunjiayi_prescription limit #{startIndex}, #{pageSize}")
    @ResultMap("prescriptionMap")
    List<YunjiayiPrescription> selectLimit(@Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize);

    /*通过id查询处方状态*/
    @Select("select prescriptionstatus from yunjiayi_prescription where prescriptionID = #{prescriptionid}")
    String selectprescriptionstatus(@Param("prescriptionid") Integer prescriptionid);


    @Update({
            "update yunjiayi_prescription",
            "set patientID = #{patientid,jdbcType=INTEGER},",
            "doctorID = #{doctorid,jdbcType=INTEGER},",
            "medicine = #{medicine,jdbcType=VARCHAR},",
            "decription = #{decription,jdbcType=VARCHAR},",
            "prescriptionstatus = #{prescriptionstatus,jdbcType=VARCHAR}",
            "where prescriptionID = #{prescriptionid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(YunjiayiPrescription record);
}